/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-10-11 08:47:43
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:39:41
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import {
    ReNewInventoryInput, StoreFunctionsServiceProxy, StorePutServiceProxy,
    StoreInfoServiceProxy, PartsDeliveryServiceProxy, PartData
} from '@shared/service-proxies/service-proxies';


@Component({
    selector: 'app-inventory',
    templateUrl: './inventory.component.html',
    styleUrls: ['./inventory.component.less']
})
export class InventoryComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    lotNo = undefined;
    productName = undefined;
    module = undefined;
    materialsNodeId = undefined;
    materialsNodeOptions = [];
    materialCategoryId = undefined;
    materialCategoryOptions = [];
    materialPropertyId = undefined;
    materialPropertyOptions = [];
    certificateNo = undefined;
    releaseNo = undefined;
    tableData = [];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
        private _storeInfoServiceProxy: StoreInfoServiceProxy,
        private _partsDeliveryServiceProxy: PartsDeliveryServiceProxy,
    ) {
        super(injector);
    }
    contChange(rowIndex) {
        this.tableData[rowIndex].differenceCount = this.tableData[rowIndex].inventoryNo - this.tableData[rowIndex].storeQuantity;
    }
    save() {
        let input = new ReNewInventoryInput();
        input.inventoryDocumentId = this.editData.id;
        input.parts = [];
        for (const item of this.tableData) {
            let data = new PartData();
            data.id = item.id;
            data.inventoryNo = item.inventoryNo;
            data.inventoryPartNo = item.inventoryPartNo;
            data.remark = item.remark;
            input.parts.push(data);
        }
        if (this.saveFlag) {
            this.saveFlag = false;
            this._partsDeliveryServiceProxy.reNewInventory(input)
                .pipe(finalize(() => { this.active = false; }))
                .subscribe((rs) => {
                    this.saveFlag = true;
                    if (rs.code == 0) {
                        this._messageService.success('盘点成功');
                        this.modalSave.emit();
                        this.close();
                    } else {
                        this._messageService.error(rs.message);
                    }
                });
        }
    }
    getTableList(event?: LazyLoadEvent) {
        this._partsDeliveryServiceProxy.getInventory(
            this.editData.id,
            this.lotNo,
            this.productName,
            this.module,
            this.materialCategoryId,
            this.materialsNodeId,
            this.materialPropertyId,
            this.certificateNo,
            this.releaseNo,
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.tableData = result;
        });
    }
    ngOnInit() {
        //物料类别
        this._storeFunctionsServiceProxy.getMaterialsTypeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialsNodeOptions = [];
                this.materialsNodeOptions = result;
            });
        this._partsDeliveryServiceProxy.getMaterialsPropertysDesc(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialPropertyOptions = [];
                this.materialPropertyOptions = result;
            });
    }
    onChange(event) {
        if (this.materialsNodeId != undefined) {
            //物料细类
            this._storeFunctionsServiceProxy.getSegmentationMaterialsTypeDropList(
                this.materialsNodeId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.materialCategoryOptions = [];
                    this.materialCategoryOptions = result;
                });
        }

    }
    //打开弹窗
    onShown(): void {
        this.active = true;
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.tableData = [];
        this.active = false;
        this.modal.hide();
        this.reset();
    }
    reset() {
        this.lotNo = undefined;
        this.productName = undefined;
        this.module = undefined;
        this.materialsNodeId = undefined;
        this.materialCategoryId = undefined;
        this.materialCategoryOptions = [];
        this.materialPropertyId = undefined;
        this.certificateNo = undefined;
        this.releaseNo = undefined;
    }

}
